# -*- coding: utf-8 -*-
# @Author : gongyu
# @Date   : 2019/4/8
# @File   : EC08_UnpaidCreditAndLoanCnt.py
# @E-mail : gongyu@bbdservice.com

'''eof
name:未结清信贷笔数
code:EC08_UnpaidCreditAndLoanCnt_2D
tableName:
columnName:
groups:企业征信模块
dependencies:EC_DSJ_2D
type:常用指标
dataSourceType:
eof'''


"""
FACTOR1 =【未结清借贷交易汇总信息段】(EB02A)-其他借贷交易分类汇总信息(EB02AH)·业务类型(EB02AD01)∈(1(中长期借款),2(短期借款),3(循环透支))且资产质量分类（EB02AD02）！=0的账户数(EB02AS04)
FACTOR2 = 【未结清担保交易汇总信息段】(EB03A)-未结清担保交易汇总信息(EB03AH)·资产质量分类（EB03AD02）！=0的账户数(EB03AS02)加总
FACTOR = FACTOR1 + FACTOR2
"""



import sys
reload(sys)
sys.setdefaultencoding('utf-8')

null_type_list = ['', ' ', None, 'null', 'Null', 'NULL', '/', '-', '--']

def getTable(data, tableName, occurTimes):
    '''
        获取表信息
        :param data:表上层的字典对象
        :param tableName:表的名字
        :param occurTimes:表出现的次数，如果是0代表此表可以不出现，如果是1代表此表必须出现
        :return:该表全部数据
        '''
    table = data.get(tableName)
    if occurTimes == 0:
        if table is None:
            return [{}]
        elif table == "":
            return ""
        elif isinstance(table, list) is False or len(table) == 0:
            return None
        else:
            return data.get(tableName)

    if occurTimes == 1:
        if table == "":
            return ""
        if table is None or isinstance(table, list) is False or len(table) == 0:
            return None
        elif len(table) == 0:
            return [{}]
        else:
            return data.get(tableName)

def isfloat(x):
    '''
    判断x为float类型且大于等于0
    :param x:
    :return:
    '''
    try:
        if isinstance(float(x), float) and float(x)>=0:
            if str(x) in ['inf', 'infinity', 'INF', 'INFINITY', 'True']:
                return False
            else:
                return True
        else:
            return False
    except:
        return False


def EC08_UnpaidCreditAndLoanCnt():
    data = EC_DSJ_2D['data']
    # 借贷交易汇总信息   [1..1]
    EBB = getTable(data , 'EBB', 1)
    if EBB is None:
        factor1 = None
    elif EBB == [{}] or EBB == "":
        factor1 = 0
    else:
        # 借贷交易汇总信息单元   [0..1]
        EB02 = getTable(EBB[0], 'EB02', 0)
        if EB02 in null_type_list:
            factor1 = None
        elif EB02 == [{}]:
            factor1 = 0
        else:
            # 未结清借贷交易汇总信息段   [1..1]
            EB02A = getTable(EB02[0], "EB02A", 1)
            if EB02A in null_type_list:
                factor1 = None
            elif EB02A == [{}]:
                factor1 = 0
            else:
                # 其他借贷交易分类汇总信息   [0..20]
                EB02AH = getTable(EB02A[0], "EB02AH", 0)
                if EB02AH in null_type_list:
                    factor1 = None
                elif EB02AH == [{}]:
                    factor1 = 0
                else:
                    factor1 = []
                    for i in EB02AH:
                        try:
                            # 业务类型   [1..1]
                            EB02AD01 = i.get('EB02AD01')
                            # EB02AD02
                            EB02AD02 = i.get("EB02AD02")
                            # 账户数   [1..1]
                            EB02AS04 = i.get('EB02AS04')
                            if EB02AD01 in null_type_list or EB02AS04 in null_type_list or EB02AD02 in null_type_list:
                                factor1.append(None)
                            # 1-中长期借款，2-短期借款，3-循环透支,4-贴现
                            elif EB02AD01 in ['1', '2', '3', '4'] and isfloat(EB02AS04) and EB02AD02 not in [0, "0"]:
                                factor1.append(float(EB02AS04))
                            else:
                                factor1.append(0)
                        except:
                            factor1.append(None)
                    factor1 = filter(lambda x: x is not None, factor1)
                    if len(factor1) == 0:
                        factor1 = None
                    else:
                        factor1 = sum(factor1)

    EBC = getTable(data, 'EBC', 1)
    if EBC is None:
        factor2 = None
    elif EBC == [{}] or EBC == "":
        factor2 = 0
    else:
        # 担保交易汇总信息单元   [0..1]
        EB03 = getTable(EBC[0], 'EB03', 0)
        if EB03 in null_type_list:
            factor2 = None
        elif EB03 == [{}]:
            factor2 = 0
        else:
            # 未结清担保交易汇总信息段   [1..1]
            EB03A = getTable(EB03[0], "EB03A", 1)
            if EB03A in null_type_list:
                factor2 = None
            elif EB03A == [{}]:
                factor2 = 0
            else:
                # 未结清担保交易汇总信息   [0..16]
                EB03AH = getTable(EB03A[0], "EB03AH", 0)
                if EB03AH in null_type_list:
                    factor2 = None
                elif EB03AH == [{}]:
                    factor2 = 0
                else:
                    factor2 = []
                    for i in EB03AH:
                        try:
                            # 账户数   [1..1]
                            EB03AS02 = i.get('EB03AS02')
                            EB03AD02 = i.get('EB03AD02')
                            if EB03AS02 in null_type_list or EB03AD02 in null_type_list:
                                factor2.append(None)
                            elif isfloat(EB03AS02) and EB03AD02 not in [0, '0']:
                                factor2.append(float(EB03AS02))
                            else:
                                factor2.append(0)
                        except:
                            factor2.append(None)
                    factor2 = filter(lambda x: x is not None, factor2)
                    if len(factor2) == 0:
                        factor2 = None
                    else:
                        factor2 = sum(factor2)

    if factor1 is None and factor2 is None:
        return u'缺失值'
    elif factor1 is None and factor2 is not None:
        return factor2
    elif factor1 is not None and factor2 is None:
        return factor1
    else:
        return factor1 + factor2

result = EC08_UnpaidCreditAndLoanCnt()

